package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Validated
@Api(tags = "02.微博模块")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;

    /**
     * 发布微博功能
     * 接口地址: /v1/weibo/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int
     */
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, HttpSession session) {
        //获取登录用户VO类
        UserVO userVO = (UserVO) session.getAttribute("user");
        //UserVO 不为null，则说明处于登录状态
        //UserVO 为null，则说明处于未登录状态
        if (userVO == null) {
            return new JsonResult(StatusCode.OPERATION_FAILED);//未登录，发布失败
        }
        //创建weibo实体类
        Weibo weibo = new Weibo();
        //复制dto类的属性至实体类
        BeanUtils.copyProperties(weiboDTO, weibo);
        //将当前登录用户的UserId放入实体类
        weibo.setUserId(userVO.getId());
        //将实体类放入数据库
        weiboMapper.insertWeiboByUid(weibo);
        return JsonResult.ok();//发布成功
    }

    /**
     * 首页微博列表功能
     * 接口地址:/v1/weibo/selectIndex
     * 请求方式:GET
     * 请求数据类型:application/x-www-form-urlencoded
     * 响应数据类型:见响应参数
     */
    @ApiOperation(value = "首页微博列表功能")
    @GetMapping("selectIndex")
    public JsonResult selectIndex() {
        List<WeiboListVO> list = weiboMapper.selectWeiList();
        return JsonResult.ok(list);
    }

    /**
     * 微博详情功能
     * 接口地址:/v1/weibo/selectById
     * 请求方式:GET
     * 请求数据类型:application/x-www-form-urlencoded
     * 响应数据类型:见响应参数
     */
    @ApiOperation(value = "微博详情功能")
    @GetMapping("selectById")
    //ApiImplicitParams：声明参数方式接收的Knife4j文档描述.
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "微博编号", required = true, example = "200", dataType = "int"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string")
    })
    //username参数仅仅是为了Knife4j中注解测试,没有任何其他作用.
    public JsonResult selectById(@Range(min = 200, max = 299, message = "微博编号必须在200到299之间") @RequestParam int id, String username) {
        //手动抛出异常,用于全局异常处理器的讲解测试;
        if (id < 0){
            throw new IllegalArgumentException("ID值无效~~");
        }

        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiDetail(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
